package pikes.peak;

import java.util.Map;

import org.springframework.web.servlet.support.RequestContext;

import pikes.ecma.SourceElements;
import pikes.html.xhtml.Inline;
import pikes.html.xhtml.form.Input;

public class InputText implements InlineComponent {

	private CharSequence name = null;

	private RequestProperty<CharSequence> valueProvider = null;

	private RequestProperty<SourceElements> onChange = null;

	public InputText() {
	}

	public InputText(final CharSequence name) {
		this(name, null);
	}

	public InputText(final RequestProperty<CharSequence> valueProvider) {
		this(null, valueProvider);
	}

	public InputText(final CharSequence name, final RequestProperty<CharSequence> valueProvider) {
		this.name = name;
		this.valueProvider = valueProvider;
	}

	public final Inline createHtml(Map model, RequestContext requestContext) throws Exception {
		Input input = null;

		if (valueProvider == null) {
			input = new Input(Input.InputType.TEXT);
		}
		else {
			input = new Input(Input.InputType.TEXT, valueProvider.getValue(model, requestContext));
		}

		if (name != null) {
			input.setName(name);
		}

		if (onChange != null) {
			input.setOnChange(onChange.getValue(model, requestContext));
		}

		return input;
	}

	public final void setOnChange(SourceElements script) {
		this.setOnChange(new RequestIndependentProperty<SourceElements>(script));
	}

	public final void setOnChange(RequestProperty<SourceElements> script) {
		this.onChange = script;
	}
}
